package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.common.admin.utils.AdminUserPermissionUtils;
import com.zhentao.model.common.dtos.PermissionResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.pojo.core.ChargeStationGun;
import com.zhentao.model.pojo.core.dto.ChargeCostDto;
import com.zhentao.model.pojo.core.dto.ChargeStationInfoDto;
import com.zhentao.model.pojo.core.vo.ChargeQrCodeVo;
import com.zhentao.service.ChargeStationGunService;
import com.zhentao.mapper.ChargeStationGunMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

import static com.zhentao.common.admin.enums.AdminPermsEnum.*;

/**
 * @author 31919
 * @description 针对表【charge_station_gun】的数据库操作Service实现
 * @createDate 2024-12-22 19:57:12
 */
@Service
public class ChargeStationGunServiceImpl extends ServiceImpl<ChargeStationGunMapper, ChargeStationGun>
        implements ChargeStationGunService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ResponseResult getCoreGunList(ChargeStationInfoDto chargeStationInfoDto) {
        List<ChargeStationGun> list = this.list(Wrappers.<ChargeStationGun>lambdaQuery().eq(ChargeStationGun::getStationId, chargeStationInfoDto.getId()).eq(ChargeStationGun::getRemark,0));
        if (list.size() <= 0) {
            return ResponseResult.errorResult(500, "暂无枪机信息");
        }

        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult CoreGetGun(ChargeStationGun chargeStationGun) {

        if (!StringUtils.isNotBlank(chargeStationGun.getId().toString()) && !StringUtils.isNotBlank(chargeStationGun.getStationId().toString())) {
            return ResponseResult.errorResult(500, "参数不能为空");
        }
        ChargeStationGun chargeGun = this.getOne(Wrappers.<ChargeStationGun>lambdaQuery().eq(ChargeStationGun::getId, chargeStationGun.getId()).eq(ChargeStationGun::getStationId, chargeStationGun.getStationId()));
        if (chargeGun == null) {
            return ResponseResult.errorResult(500, "参数错误");
        }
        return ResponseResult.okResult(chargeGun);
    }

    @Override
    public ResponseResult CoreCalculate(ChargeCostDto chargeCostDto) {
        if (StringUtils.isNotBlank(chargeCostDto.getPrice().toString()) && !StringUtils.isNotBlank(chargeCostDto.getBatteryCapacity().toString())) {
            return ResponseResult.errorResult(500, "参数错误");
        }
        Integer batteryCapacity = chargeCostDto.getBatteryCapacity();
        Double price = chargeCostDto.getPrice();
        Double cost = batteryCapacity * price;
        cost = Math.round(cost * 100.0) / 100.0; // 保留两位小数
        return ResponseResult.okResult(cost);
    }

    @Override
    public ResponseResult CoreGunUpdate(ChargeStationGun chargeStationGun) {
        if (!StringUtils.isNotBlank(chargeStationGun.getId().toString()) && !StringUtils.isNotBlank(chargeStationGun.getStationId().toString())) {
            return ResponseResult.errorResult(500, "参数错误");
        }
        ChargeStationGun one = this.getOne(Wrappers.<ChargeStationGun>lambdaQuery().eq(ChargeStationGun::getId, chargeStationGun.getId()).eq(ChargeStationGun::getStationId, chargeStationGun.getStationId()));
        if (one == null) {
            return ResponseResult.errorResult(500, "参数错误");
        }

        if (one.getStatus() == 0) {
            chargeStationGun.setStatus(1);
            boolean update = this.update(Wrappers.<ChargeStationGun>lambdaUpdate().set(ChargeStationGun::getStatus, chargeStationGun.getStatus()).eq(ChargeStationGun::getId, chargeStationGun.getId()).eq(ChargeStationGun::getStationId, chargeStationGun.getStationId()));
            if (update) {
                return ResponseResult.okResult(200, "修改成功");
            }
        }

        if (one.getStatus() == 1) {
            chargeStationGun.setStatus(0);
            boolean update = this.update(Wrappers.<ChargeStationGun>lambdaUpdate().set(ChargeStationGun::getStatus, chargeStationGun.getStatus()).eq(ChargeStationGun::getId, chargeStationGun.getId()).eq(ChargeStationGun::getStationId, chargeStationGun.getStationId()));
            if (update) {
                return ResponseResult.okResult(200, "修改成功");
            }
        }

        return ResponseResult.errorResult(500, "修改失败");
    }

    @Override
    public ResponseResult resolveQRCode(String qrCode) {
        HashMap<String, Object> map = new HashMap<>();
        String[] split = qrCode.split(",");
        for (String s : split) {
            String[] split1 = s.split(":");
            map.put(split1[0], split1[1]);
        }
        ChargeQrCodeVo chargeQrCodeVo = new ChargeQrCodeVo();
        chargeQrCodeVo.setId((Integer) map.get("id"));
        chargeQrCodeVo.setStationId((Integer) map.get("stationId"));
        chargeQrCodeVo.setPrice(Double.valueOf((String) map.get("price")));
        chargeQrCodeVo.setBatteryCapacity((Double) map.get("batteryCapacity"));
        chargeQrCodeVo.setVehicleIdentifyNo((String) map.get("vehicleIdentifyNo"));
        return ResponseResult.okResult(chargeQrCodeVo);
    }

    @Override
    public PermissionResult getAdminCoreGunList(ChargeStationInfoDto chargeStationInfoDto) {
        AdminUserPermissionUtils permissionUtils = new AdminUserPermissionUtils(redisTemplate);
        ResponseResult result = permissionUtils.hasPermission(SYS_SITE_SELECT);
        if(!(result.getCode() == 200000)){
            return PermissionResult.ERROR(result.getCode(),result.getErrorMessage());
        }
        if (chargeStationInfoDto==null) {
            return PermissionResult.ERROR(400000, "参数错误");
        }
        List<ChargeStationGun> list = this.list(Wrappers.<ChargeStationGun>lambdaQuery().eq(ChargeStationGun::getStationId, chargeStationInfoDto.getId()).eq(ChargeStationGun::getRemark, 0));
        if (list.size() <= 0) {
            return PermissionResult.ERROR(400000, "暂无枪机信息");
        }

        return PermissionResult.OK(200000, "查询成功",list);
    }

    @Override
    public PermissionResult AdminGunDel(ChargeStationGun chargeStationGun) {
        AdminUserPermissionUtils permissionUtils = new AdminUserPermissionUtils(redisTemplate);
        ResponseResult result = permissionUtils.hasPermission(SYS_SITE_DEL);
        if(!(result.getCode() == 200000)){
            return PermissionResult.ERROR(result.getCode(),result.getErrorMessage());
        }
        if (chargeStationGun==null){
            return PermissionResult.ERROR(400000, "参数错误");
        }
        boolean update = this.update(Wrappers.<ChargeStationGun>lambdaUpdate().set(ChargeStationGun::getRemark, 1).eq(ChargeStationGun::getId, chargeStationGun.getId()));
        if (update){
            return PermissionResult.OK(200000, "删除成功");
        }

        return PermissionResult.ERROR(400000, "删除失败");
    }

    @Override
    public PermissionResult AdminCoreGunUpdate(ChargeStationGun chargeStationGun) {
        AdminUserPermissionUtils permissionUtils = new AdminUserPermissionUtils(redisTemplate);
        ResponseResult result = permissionUtils.hasPermission(SYS_SITE_EDIT);
        if(!(result.getCode() == 200000)){
            return PermissionResult.ERROR(result.getCode(),result.getErrorMessage());
        }
        if (chargeStationGun==null){
            return PermissionResult.ERROR(400000, "参数错误");
        }
        boolean b = this.updateById(chargeStationGun);
        if (b){
            return PermissionResult.OK(200000, "修改成功");
        }
        return PermissionResult.ERROR(400000, "修改失败");
    }
}




